package com.wenyl.juc.thread.semaphoreTest;

import java.util.concurrent.Semaphore;

/**
 * @author Swimming Dragon
 * @description: TODO
 * @date 2024年11月07日 11:13
 */
public class Address {
    //每次停车数量
    private int num;
    // 信号量
    private Semaphore semaphore;
    public Address(int num){
        this.num = num;
        this.semaphore = new Semaphore(this.num);
    }
    public void autoCar(){
        try {
            this.semaphore.acquire();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName()+"，进入停车场");
        try {
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName()+"，离开停车场");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.semaphore.release();
    }
}
